#include "Hand.h"
#include "Card.h"

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include"QtCore"
using namespace std;

Hand::Hand()
{

}
Hand::~Hand()
{

}

void Hand::addCard(Card c)
{

    cards.push_back(c);
}

void Hand::removeCard(Card c)
{
   // qDebug()<<"remove card from "<<c.owner;
   // c.print();
    cards.remove(c);
}

int Hand::getCardIndex(int id)
{
   int index =0;
   for(list<Card>::iterator pos = cards.begin(); pos != cards.end();pos++)
   {
        if(pos->id == id)
           return index;

        index++;
   }

    return -1;
}
Card* Hand::getCard(int index)
{
   list<Card>::iterator it = cards.begin();
   advance(it,index);
   return &(*it);

}
int Hand::numberOfSuit(char s)
{
    int counter= 0;
    for(list<Card>::iterator pos = cards.begin(); pos != cards.end();pos++)
    {
        if(pos->suit == s)
            counter++;
    }
    return counter;
}

Card* Hand::getMaxCard()
{
    /*
    Card max = cards.front();

    for(list<Card>::iterator pos = cards.begin(); pos != cards.end();pos++)
    {

        if(max < *pos)
            max = *pos;
    }

    return &max;*/

    Card *max=NULL;

    for(int i=0;i<getHandSize();i++)
    {
        if(max==NULL)
            max=getCard(i);

        if(*(max)<*(getCard(i)))
        {
            max=getCard(i);
        }
    }

    return max;
}

Card* Hand::getMinCard()
{
    /*Card min = cards.front();
    for(list<Card>::iterator pos = cards.begin(); pos != cards.end();pos++)
    {
        if(min > *pos)
            min = *pos;
    }
    return &min;*/
    Card *min=NULL;

    for(int i=0;i<getHandSize();i++)
    {
        if(min==NULL)
            min=getCard(i);

        if(*(min)>*(getCard(i)))
        {
            min=getCard(i);
        }
    }

    return min;
}
int Hand::determineWinner()
{
   return getMaxCard()->owner;
}

void Hand:: print()
{
    for(list<Card>::iterator pos = cards.begin(); pos != cards.end();pos++)
    {
          pos->print();

    }

}
int Hand::getHandSize()
{
    return cards.size();
}
void Hand::updateHandInfo()
{
    score=calcHandScore();

    balanced=true;


    int longest=-1;
    int temp=0;
    int numDouble=0;
    string suits="SHCD";
    for(int i=0;i<4;i++)
    {
         temp= numberOfSuit(suits[i]);
         if(temp==0 || temp==1)
             balanced=false;
         if(temp==2)
             numDouble++;
         if(numDouble>1)
             balanced=false;

        if(longest<temp)
        {
            longestSuit=suits[i];
            longest=temp;
        }
    }
    /*cout<<"score = "<<score;
    cout<<"balanced = "<<balanced;
    cout<<"longest suit = "<<longestSuit;*/

}

int Hand::calcHandScore()
{
    Card *temp;
    int temp_handScore=0;

    for(int i=0;i<getHandSize();i++)
    {
        temp=getCard(i);
        switch (temp->toNumber())
        {
            case 14:
            {
                 temp_handScore=temp_handScore+4;
                 break;
            }
            case 13:
            {
                 temp_handScore=temp_handScore+3;
                 break;
            }
            case 12:
            {
                 temp_handScore=temp_handScore+2;
                 break;
            }
            case 11:
            {
                 temp_handScore=temp_handScore+1;
                 break;
            }
        };
    }

    int temp2=0;
    string suits="SHCD";
    for(int i=0;i<4;i++)
    {
       temp2= numberOfSuit(suits[i]);

        if(temp2>=5)
        {

           temp_handScore=temp_handScore+1;
           temp_handScore=temp_handScore+(temp2-5);
        }
    }

    return temp_handScore;
}
int Hand::getHandScore(){return score;}
bool Hand::isBalanced(){return balanced;}
char Hand::getLongestSuit(){return longestSuit;}

void Hand::updateOwner(int)
{

}
 void Hand::sortHand()
 {
        qDebug()<<"sorting hand";
        list <Card> clubs;
        list <Card> diamonds;
        list <Card> hearts;
        list <Card> spades;

        list<Card>  newList;

       for(int i=0;i<getHandSize();i++)
           if(getCard(i)->suit=='C')
               clubs.push_front(*getCard(i));
       clubs.sort();
       for(int i=0;i<getHandSize();i++)
           if(getCard(i)->suit=='D')
               diamonds.push_front(*getCard(i));
       diamonds.sort();
       for(int i=0;i<getHandSize();i++)
           if(getCard(i)->suit=='H')
               hearts.push_front(*getCard(i));
       hearts.sort();
       for(int i=0;i<getHandSize();i++)
           if(getCard(i)->suit=='S')
               spades.push_front(*getCard(i));
       spades.sort();

       for(list<Card>::iterator pos = clubs.begin(); pos != clubs.end();pos++)
           newList.push_front(*pos);
       for(list<Card>::iterator pos = diamonds.begin(); pos != diamonds.end();pos++)
           newList.push_front(*pos);
       for(list<Card>::iterator pos = hearts.begin(); pos != hearts.end();pos++)
           newList.push_front(*pos);
       for(list<Card>::iterator pos = spades.begin(); pos != spades.end();pos++)
           newList.push_front(*pos);

      cards=newList;
 }

